package com.example.base.flow.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.flow.domain.SysFlow;
import com.example.base.flow.domain.vo.SysFlowVo;
import com.example.base.flow.mapper.SysFlowMapper;
import com.example.base.flow.service.SysFlowService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.cache.annotation.*;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.CacheConfig;

import java.io.Serializable;
import java.util.Date;

/**
 * <p>
 * 工作流 服务实现类
 * </p>
 *
 * @author X.I.O
 * @since 2024-04-25
 */
@Service
@Transactional
@CacheConfig(cacheNames = "SysFlowServiceImpl")
public class SysFlowServiceImpl extends ServiceImpl<SysFlowMapper, SysFlow> implements SysFlowService {

    @Cacheable(cacheNames = "sysflow",key = "#id")
    @Override
    public SysFlow getById(Serializable id){
        return super.getById(id);
    }

    @CacheEvict(cacheNames = "sysflow", key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @CacheEvict(cacheNames = "sysflow", key = "#sysflow.id")
    @Override
    public boolean updateById(SysFlow sysflow) {
        return super.updateById(sysflow);
    }

    @CachePut(cacheNames = "sysflow", key = "#sysflow.id")
    public SysFlow saveSysFlow(SysFlow sysflow) throws GlobalExcetion {
        super.save(sysflow);
        return sysflow;
    }

    @CacheEvict(cacheNames = "sysflow", key = "#sysflow.id")
    public SysFlow updateSysFlowById(SysFlow sysflow) throws GlobalExcetion{
        super.updateById(sysflow);
        return sysflow;
    }


    public SysFlow saveOrUpdateSysFlow(SysFlowVo sysflowVo) throws GlobalExcetion{
        SysFlow sysflow=new SysFlow();
        BeanUtils.copyProperties(sysflowVo,sysflow);
        if(sysflowVo.getId()!=null){
            SysFlow sysflowEntity=super.baseMapper.selectById(sysflowVo.getId());
            if(sysflowEntity==null){
                throw new GlobalExcetion(1,"数据不存在");
            }
            sysflow.setUpdateDate(new Date());
            super.baseMapper.updateById(sysflow);
        }else{
            String json=sysflowVo.getFlowJson();
            JSONObject object=JSONObject.parseObject(json);
            String name=object.getString("name");
            sysflow.setFlowName(name);
            sysflow.setCreateDate(new Date());
            sysflow.setUpdateDate(new Date());
            sysflow.setFlowVersion("1.00");
            super.baseMapper.insert(sysflow);
        }
        return sysflow;
    }

    public IPage<SysFlow> page(Integer page, Integer pageSize) throws GlobalExcetion{
        LambdaQueryWrapper<SysFlow> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        IPage<SysFlow> iPage=super.baseMapper.selectPage(new Page(page,pageSize),lambdaQueryWrapper);
        return iPage;
    }
}
